﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;

namespace Pixysoft.Framework.Configurations.Core
{
    class XmlLessConstraintProperty<T> : XmlConstraintProperty<T>, IXmlConstraintProperty
    {
        public XmlLessConstraintProperty(string elementName,
            string propertyName, T propertyValue, string format)
            : base(elementName, propertyName, propertyValue, format)
        {
        }

        #region IXmlConstraintProperty 成员

        public bool Meets(string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;

            switch (ParserHelper.GetDataType(PropertyValue))
            {
                case DotNetDataType.Boolean:
                    {
                        return false;
                    }
                case DotNetDataType.Char:
                    {
                        char tvalue = char.MinValue;

                        if (!ParserHelper.TryParse<char>(value, out tvalue))
                            return false;

                        char lvalue = (char)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.DateTime:
                    {
                        DateTime tvalue = DateTime.MinValue;

                        if (!ParserHelper.TryParse<DateTime>(value, out tvalue, Format))
                            return false;

                        DateTime lvalue = (DateTime)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Decimal:
                    {
                        decimal tvalue = decimal.MinValue;

                        if (!ParserHelper.TryParse<decimal>(value, out tvalue))
                            return false;

                        decimal lvalue = (decimal)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Double:
                    {
                        double tvalue = double.MinValue;

                        if (!ParserHelper.TryParse<double>(value, out tvalue))
                            return false;

                        double lvalue = (double)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Int32:
                    {
                        Int32 tvalue = Int32.MinValue;

                        if (!ParserHelper.TryParse<Int32>(value, out tvalue))
                            return false;

                        Int32 lvalue = (Int32)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Int64:
                    {
                        Int64 tvalue = Int64.MinValue;

                        if (!ParserHelper.TryParse<Int64>(value, out tvalue))
                            return false;

                        Int64 lvalue = (Int64)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Single:
                    {
                        Single tvalue = Single.MinValue;

                        if (!ParserHelper.TryParse<Single>(value, out tvalue))
                            return false;

                        Single lvalue = (Single)(GenericPropertyValue as object);

                        return tvalue < lvalue;
                    }
                case DotNetDataType.Byte:
                case DotNetDataType.String:
                    {
                        return false;
                    }
                default:
                    {
                        return false;
                    }
            }
        }

        #endregion
    }
}
